# 取F10数据
# 自动切换成F10页面, 然后再切换回来


import time
import cv2
import numpy as np
import pyautogui
import logging
from PIL import ImageGrab


class module_get_F10_data:
    def __init__(self):
        logging.info("init module_get_F10_data")
        self.init_done = False
        self.pos_title_1 = (0,0,0,0) # >最新指标
        self.pos_title_2 = (0,0,0,0) # >大事提醒
        self.template_F10_button_idle = cv2.imread("eastmoney desktop software picture/F10_button_idle.png")
        self.template_F10_button_pushed = cv2.imread("eastmoney desktop software picture/F10_button_pushed.png")

    def init_2(self,efortune_window):
        self.efortune_window = efortune_window
        self.init_done = True

    # 屏幕截图
    def capture_window(self):
        try:
            # 获取窗口位置和大小
            x, y = self.efortune_window.left, self.efortune_window.top
            width, height = self.efortune_window.width, self.efortune_window.height
            self.window_pos = (x, y, width, height)
            # 截取窗口区域
            screenshot = ImageGrab.grab(bbox=(x, y, x + width, y + height))
            # 转换为OpenCV格式 (BGR)
            self.frame = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)            
        except Exception as e:
            logging.error(f"!!!! ERR 36 F10截图时出错: {str(e)}")
            return None
    # 找 模板匹配 返回坐标与大小
    def find_template_position(self,screen_img, template_img, threshold=0.85):
        
        """获取所有符合阈值的匹配结果，返回列表[(x, y, w, h, 匹配度)]"""
        # # 预处理（使用之前优化的预处理函数）
        h, w = template_img.shape[:2]
         
        if h == 0 or w == 0:
            return []
        
        if screen_img.size < template_img.size:  # 截图尺寸小于模板尺寸
            # print("截图尺寸小于模板尺寸")
            return []
        
        # 执行模板匹配并获取匹配度矩阵
        result = cv2.matchTemplate(
            screen_img, 
            template_img, 
            cv2.TM_CCOEFF_NORMED
        )
        # 找到所有超过阈值的位置
        locations = np.where(result >= threshold)
        all_matches = []
        
        # 遍历所有匹配位置，记录坐标和匹配度
        for y, x in zip(locations[0], locations[1]):
            match_value = result[y, x]  # 该位置的匹配度（0-1）
            all_matches.append((x, y, w, h, match_value))  # 存储x坐标、y坐标、宽、高、匹配度
            
        if not all_matches:
            return None  # 无匹配结果
        # 按X坐标升序排序（X越小越靠左），取第一个
        # 若X相同，可选匹配度最高的
        all_matches.sort(key=lambda m: (-m[4]))  # 匹配度倒序排序
        most_matched = all_matches[0]
        return (most_matched[0], most_matched[1], most_matched[2], most_matched[3])  # 返回(x, y, w, h)
    
    
    template_img_title_1 = cv2.imread("eastmoney desktop software picture/F10_title_1_new_data.png")
    template_img_title_2 = cv2.imread("eastmoney desktop software picture/F10_big_news_nitice.png")
    template_img_title_special = cv2.imread("eastmoney desktop software picture/F10_title_special.png")
    template_img_title_3 = cv2.imread("eastmoney desktop software picture/F10_table_header_0.png")


    # 刚按F10的第一个页面, 有[大事提醒]这个小标题
    def have_data_and_load_done_1(self):
        
        pos1 = self.find_template_position(self.frame, self.template_img_title_1)
        pos2 = self.find_template_position(self.frame, self.template_img_title_2)
        if None == pos2:
            pos2 = self.find_template_position(self.frame, self.template_img_title_special)

        if pos1 == None or pos2 == None:
            return False

        self.pos_title_1 = pos1
        self.pos_title_2 = pos2

        return True

    # 进一步判断已加载数据:
    def have_data_and_load_done_2(self):
        pos1 = self.find_template_position(self.frame, self.template_img_title_3)
        
        if pos1 == None:
            return False
        return True

    def is_F10_button_pressed_press_if_not_pressed(self):
        # 1. 裁剪窗口, 只出左边宽40像素的竖条.
        img_left = self.frame[:, :40]
        # 2. 找已按下的状态
        match_pos = self.find_template_position(img_left, self.template_F10_button_pushed)
        # 3. 没按就按下
        if match_pos != None:
            # pyautogui.press('f10')
            # 移动鼠标到按钮中心:
            pyautogui.moveTo(self.window_pos[0]+match_pos[0]+match_pos[2]-20,
                              self.window_pos[1]+match_pos[1]+match_pos[3]*0.5, duration=0.0)
            pyautogui.click()
            return True
    
    def get_data_from_pageone(self):
        # print("# 120 get_data_from_pageone BEGIN")
        # 通过两个小标题裁剪窗口
        img_between_title = self.frame[self.pos_title_1[1]+self.pos_title_1[3]:self.pos_title_2[1], self.pos_title_1[0]-10:]
        # print(f"# 126 y0:{self.pos_title_1[1]+self.pos_title_1[3]} \t y1:{self.pos_title_2[1]}")
        height_img_between_title,width_img_between_title = img_between_title.shape[:2]
        # print(f"# 126 height_img_between_title:{height_img_between_title} \t width_img_between_title:{width_img_between_title}")
        # cv2.imwrite("F10_title_1_to_title_2.png", img_between_title)
        # 切掉尾部:
        h_between_title = img_between_title.shape[0]
        w_between_title = img_between_title.shape[1]
        # print(f"# 131 h_between_title:{h_between_title} \t w_between_title:{w_between_title}")

        # 找黑色列的x坐标:
        black_column=0
        for x in range(w_between_title):
            bool_is_black = True
            for y in range(h_between_title):
                color_origin = img_between_title[y,x] 
                blue = color_origin[0]
                if blue > 0:
                    bool_is_black = False
                    break
                # img_between_title[y,x] = (255,255,255)
            if bool_is_black == True:
                # print(f"# 137 找到黑色列 {x}")
                black_column = x
                break
        
        # 裁剪掉黑色列之后的:
        img_table = img_between_title[: , :black_column]
        if 0 == img_table.shape[1]:
            raise Exception("!!!!    ERR 144 表格图像宽度为0")
            return None
        
        # cv2.imwrite("F10_table.png", img_table)

        # 裁剪成上下两部分:
        h_two_table = img_table.shape[0]
        w_two_table = img_table.shape[1]
        # print(f"# 158 h_two_table:{h_two_table} \t w_two_table:{w_two_table}")

        bool_last_row_is_have_data = False
        last_have_date_row = 0 # 之前的有数据的行 存Y坐标 存数据开始的起始位置
        img_table_1 = None
        img_table_2 = None
        for y in range(h_two_table):
            bool_is_all_black = True
            for x in range(w_two_table):
                color_origin = img_table[y,x] 
                blue = color_origin[0]
                if blue > 0:
                    bool_is_all_black = False
                    if False == bool_last_row_is_have_data:
                        last_have_date_row = y
                        bool_last_row_is_have_data = True
                        # print(f"# 178 切出两表的一个起始坐标: last_have_date_row:{last_have_date_row}")
                    break
            if bool_is_all_black:
                if bool_last_row_is_have_data:
                    if img_table_1 is None:
                        # print(f"# 182 表1的裁剪坐标:y1 {last_have_date_row}:y2 {y}")
                        if 64 < y-last_have_date_row:
                            img_table_1 = img_table[last_have_date_row:y,:]
                            # print(f"# 183 img_table_1 : y1 {last_have_date_row}:y2 {y}")
                            # cv2.imwrite("F10_table_1.png", img_table_1)
                        bool_last_row_is_have_data = False
                    else:
                        # print(f"# 182 表2的裁剪坐标:y1 {last_have_date_row}:y2 {y}")
                        if 64 < y-last_have_date_row:
                            img_table_2 = img_table[last_have_date_row:y,:]
                            # cv2.imwrite("F10_table_2.png", img_table_2)
                        break
        # 这里处理一个特殊情况:
        # 一般, 碰到黑边再去截取一张表, 但表2可能底下没有黑边. 也就是表2不截取, 也就是 img_table_2 会有None的情况.
        if img_table_2 is None:
            img_table_2 = img_table[last_have_date_row:,:]
            # print(f"# 198 img_talbe_2 重新赋值 last_have_date_row:{last_have_date_row}")

        # 切掉"数据来源"
        def cut_off_info_of_data_source(img_table):
            if img_table is None: 
                raise Exception("!!!!    ERR 197 img_table is None")
            try:
                h_table = img_table.shape[0]
                w_table = img_table.shape[1]
            except Exception as e:
                raise Exception("!!!!    ERR 203 h_table w_table")
            try:
                cut_y_pos = h_table
                for y in range(h_table):
                    number_of_black_pixel = 0
                    for x in range(w_table):
                        color_origin = img_table[y,x]
                        blue = color_origin[0]
                        if 0 == blue:
                            number_of_black_pixel += 1
                    if number_of_black_pixel >= w_table-2:
                        cut_y_pos = y
                        break
            except Exception as e:
                raise Exception("!!!!    ERR 217 cut_off_info_of_data_source->for")
            img_table = img_table[:cut_y_pos,:]
            # cv2.imwrite("F10_table_inner.png", img_table)
            return img_table
        try:
            # print("# 217 裁剪前 高度:",img_table_1.shape[0])
            img_table_1 = cut_off_info_of_data_source(img_table_1) 
            # print("# 219 裁剪后 高度:",img_table_1.shape[0])
            img_table_2 = cut_off_info_of_data_source(img_table_2)
        except Exception as e:
            cv2.imwrite("F10_table_ERR_221_0_img_between_title.png", img_between_title)
            cv2.imwrite("F10_table_ERR_221_0.png", img_table)
            if img_table_1 is not None:cv2.imwrite("F10_table_ERR_221_1.png", img_table_1)
            if img_table_2 is not None:cv2.imwrite("F10_table_ERR_221_2.png", img_table_2)            
            logging.info("!!!!    ERR 237 出错内容:")
            logging.info(e)
            # raise Exception("!!!!    ERR 239 cut_off_info_of_data_source 出错")
            return None

        # cv2.imwrite("F10_table_outter.png", img_table_1)

        # 从表中取数据 , 就不比对表中的字段名了. 只需取数据格子.
        # 数据格子怎么来? 数据格子切出来. 有纯黑像素就当作是数据格子.
        # 数据格子取完, 是有顺序的, 再把字段按顺序死个列表对应, 就解决了.
        # 这样的话, 表头都不用处理


        table_data_box = [] # 当前表格, 所有的数据格子.
        result_of_string_list = [] # 上面的表格解析出来的数据
        number_of_debug = 0

        # 取只有黑底的数据格子, 并去到空白区域
        def cut_every_row(img):
            _data_box = []
            h_column = img.shape[0]
            w_column = img.shape[1]
            pos_y_of_last_border = 0
            bool_last_row_have_black_pixel = False
            for y in range(h_column):
                number_of_border_pixel = 0
                for x in range(w_column):
                    b,g,r = img[y,x]
                    if (53,53,55) == (r,g,b):
                        number_of_border_pixel += 1
                        if(number_of_border_pixel >= w_column/4):
                            if y > 0 and bool_last_row_have_black_pixel:
                                y_2 = pos_y_of_last_border+1
                                pos_y_begin = y_2
                                debug_origin_y_begin = pos_y_begin
                                # BEGIN去掉上面的两空行
                                while y_2 < y :
                                    bool_is_all_black = True
                                    for x_2 in range(w_column):
                                        b,g,r = img[y_2,x_2]
                                        if (0,0,0) != (b,g,r):
                                            bool_is_all_black = False
                                            break
                                        # else:
                                        #     print(f"# 235 r,g,b:{r,g,b} x,y:{x_2,y_2}")
                                    if bool_is_all_black:
                                        pos_y_begin+=1
                                    else:
                                        break
                                    y_2+=1
                                # END  去掉上面的两空行

                                # BEGIN 去掉下面的两空行
                                y_3 = y - 1
                                end_y = y
                                debug_origin_y_end = end_y
                                while y_3 > pos_y_begin:
                                    bool_is_all_black = True
                                    for x_3 in range(w_column):
                                        b,g,r = img[y_3,x_3]
                                        if (0,0,0) != (b,g,r):
                                            bool_is_all_black = False
                                            break
                                    if bool_is_all_black:
                                        end_y-=1
                                    else:
                                        break
                                    y_3-=1
                                # END 去掉下面的两空行

                                # begin 右边的全黑空行切掉
                                x_4 = w_column - 1
                                x_end = w_column
                                while x_4 > 0:
                                    bool_is_all_black = True
                                    for y_4 in range(end_y-1,pos_y_begin-1,-1):
                                        b,g,r = img[y_4,x_4]
                                        if (0,0,0) != (b,g,r):
                                            bool_is_all_black = False
                                            break
                                    if bool_is_all_black:
                                        x_end = x_4
                                    else:
                                        break
                                    x_4-=1
                                # end 右边的全黑空行切掉

                                # print(f"# 261 图片起止Y坐标:  {pos_y_begin} : {end_y} \t 原本的起止: {pos_y_of_last_border+1} : {y}")
                                if end_y - pos_y_begin == 12:
                                    end_y-=2 # 去掉12这个高度, 为了应对有逗号没中文字符的情况.
                                img_row = img[pos_y_begin:end_y,:x_end]
                                # if 1 == img_row.shape[0]: # 12
                                #     cv2.imwrite(f"F10_table_row_ERR_291.png", img)
                                #     cv2.imwrite(f"F10_table_row_ERR_293.png", img[debug_origin_y_begin:debug_origin_y_end,:])
                                #     print(f"# 290 pos_y_begin: {pos_y_begin}\tend_y: {end_y}\tx_end: {x_end}\timg.shape: {img.shape}")
                                # if img_row.shape[0] < 10:
                                #     raise "!!!!    ERR 343 图片调试小于10"
                                _data_box.append(img_row)
                                # cv2.imwrite(f"F10_table_row_{number_of_debug}.png", img_row)
                            pos_y_of_last_border = y
                            bool_last_row_have_black_pixel = False
                            break
                    elif (0,0,0) == (r,g,b):
                        bool_last_row_have_black_pixel = True
                        break
            return _data_box

        h_table = img_table_1.shape[0]
        w_table = img_table_1.shape[1]
        pos_x_of_last_border = 0
        # str_debug_info = ""
        # 切每列:
        for x in range(w_table):
            number_of_border_pixel = 0
            for y in range(h_table):
                b,g,r = img_table_1[y,x]
                if (53,53,55) == (r,g,b):
                    number_of_border_pixel += 1
            if number_of_border_pixel >=h_table:
                # 确认这是一个边界线
                if x > 0:
                    img_column = img_table_1[:,pos_x_of_last_border+1:x]
                    if img_column.shape[1] > 350:
                        # cv2.imwrite(f"F10_table_column_ERR_356_img_table_1.png", img_table_1)
                        # cv2.imwrite(f"F10_table_column_ERR_356_img_column.png", img_column)
                        # logging.info("出错信息输出:")
                        # logging.error(str_debug_info)
                        # raise "!!!!    ERR 356 图片宽度大于350 不能作为列 这造成之后的切行错误"
                        return None # 就是还没加载完
                    _data_box = cut_every_row(img_column)
                    table_data_box.extend(_data_box)
                pos_x_of_last_border = x
            # else:
            #     str_debug_info+=f"# 318 x: {x}\tnumber_of_border_pixel: {number_of_border_pixel}\th_table:{h_table}\n"

        # ## 至此, 取得15个数据格子            
        # for row in table_data_box:
        #     cv2.imwrite(f"F10_table_row_{number_of_debug}.png", row)
        #     number_of_debug+=1

        # 从15个数据格子中, 取数据, 有亿/万这类字符 -,.
        def get_data_from_picture(img):
            number_string = ""
            last_chinese_word = ""
            
            img_height,img_width = img.shape[:2]
            if 1 == img_height:
                return None
            if img_height == 14:
                # 找亿或万:
                index_reverse_x = img_width-1
                while index_reverse_x >= 0:
                    b,g,r = img[0,index_reverse_x]
                    if b > 0:
                        # 就是万:
                        last_chinese_word = "万"
                        break
                    else:
                        # 就是亿:
                        last_chinese_word = "亿"
                        break
                    index_reverse_x -=1
                img = img[2:img_height-2,:img_width-13]
            # else: 不用else, 因为高度为14的情况, 会进一步裁剪
            # 用裁剪后的图像去取数字, 高度一定是10
            if 10 != img.shape[0]:
                cv2.imwrite(f"F10_table_row_after_cut.png", img)
                raise Exception(f"!!!!    ERR 343    图片高度不是10  {img.shape[0]}")
            img_height,img_width = img.shape[:2]
            x = 0
            while x < img_width:
                # print (f"# 356 x: {x}\t img_width: {img_width}")
                for y in [0,1,2,5,8]:
                    b,g,r = img[y,x]
                    if 0 == y and b > 0: # 5或7
                        b_2,g_2,r_2 = img[y+2,x]
                        if b_2 > 0:
                            number_string += "5"
                            x+=5
                            break
                        else:
                            number_string += "7"
                            x+=5
                            break
                    elif 1 == y and b > 0: # 1 2 3 8 9
                        b_2,g_2,r_2 = img[2,x]
                        if b_2 > 0: # 2 3 8 9
                            b_3,g_3,r_3 = img[3,x]
                            if b_3 > 0: # 8 9
                                b_4,g_4,r_4 = img[4,x]
                                if b_4 >0:
                                    number_string += "9"
                                    x+=6
                                    break
                                else:
                                    number_string += "8"
                                    x+=6
                                    break
                            else: # 2 3
                                b_7,g_7,r_7 = img[7,x]
                                if b_7 > 0: # 3
                                    number_string += "3"
                                    x+=5
                                    break
                                else: # 2
                                    number_string += "2"
                                    x+=5
                                    break
                        else:
                            number_string += "1"
                            x+=5
                            break
                    elif 2 == y and b > 0: # 0 6
                        b_4,g_4,r_4 = img[4,x+1]
                        if b_4 > 0: # 0
                            number_string += "6"
                            x+=6
                            break
                        else: # 6
                            number_string += "0"
                            x+=6
                            break
                    elif 5 == y and b > 0: # 4 -
                        # - 号的宽度为7 4的宽度为5 4后面必然有个空列
                        if img_width <=x+5:
                            number_string += "4"
                            x+=5
                            break
                        else:
                            b_5,g_5,r_5 = img[5,x+5]
                            if b_5 == 0: # 4
                                number_string += "4"
                                x+=5
                                break
                            else: # -
                                number_string += "-"
                                x+=6
                                break
                    elif 8 == y and b > 0: # . ,
                        b_9,g_9,r_9 = img[9,x] # 9 表是此列最下方一个像素点
                        if b_9 > 0: # .
                            number_string += "."
                            x+=5
                            break
                        else: # ,
                            # number_string += "," # 写到CSV文件会有麻烦
                            x+=5
                            break
                # END for
                # print(f"# 391 {number_string}")
                x+=1
            # end while
            return number_string + last_chinese_word

        # 取得文字信息:
        # number_string = get_data_from_picture(img_table_1)
        number_of_none = 0 # 计数None的个数, None超过一个, 则返回None
        for row in table_data_box:
            #cv2.imwrite(f"F10_table_row_{number_of_debug}.png", row)
            #number_of_debug+=1
            result_string =get_data_from_picture(row) 
            result_of_string_list.append(result_string)
            if None == result_string:
                number_of_none+=1
        if 4 < number_of_none: 
            # print("# 456 get_data_from_pageone return None")
            return None # 一般会缺3个数据 某些会缺4个数据
        elif 0 == len(result_of_string_list):
            # print(f"!!!!    ERR 459 table_data_box:{len(table_data_box)}\t")
            cv2.imwrite("F10_table_ERR_460.png", img_table_1)
            cv2.imwrite("F10_table_ERR_460_two_table.png", img_table)            
            raise Exception("!!!!    ERR 460    ")
            return None
        # print("# 458 get_data_from_pageone END")
        return result_of_string_list
    # END get_data_from_pageone(self)

    # K线图按钮模板 已按下的状态:
    template_k_line_button_pushed = cv2.imread("eastmoney desktop software picture/button_k_line_pushed.png")
    # K线图按钮模板 未按下的状态:
    template_k_line_button_idle = cv2.imread("eastmoney desktop software picture/button_k_line_idle.png")


    # 判断K线按钮是否已经按下
    def is_k_line_button_activied(self):
        # 取截屏的前40像素:
        img_left_40_of_screen = self.frame[:,0:40]
        # 取匹配的坐标:
        pos = self.find_template_position(img_left_40_of_screen, self.template_k_line_button_pushed)
        return pos != None

    # 鼠标点击 输入的是相对坐标
    def click_button(self,pos_x, pos_y):
        window_pos_x = self.window_pos[0]
        window_pos_y = self.window_pos[1]
        window_width = self.window_pos[2]
        # 移动鼠标到按钮中心
        pyautogui.moveTo(pos_x+window_pos_x, pos_y+window_pos_y, duration=0.0)  # duration控制移动速度，单位秒
        # 点击鼠标左键
        pyautogui.click()
        time.sleep(0.01)
        # 点击后, 把鼠标移到最右, 以防止高亮造成下去的图像比对失败
        pyautogui.moveTo(window_width+window_pos_x, pos_y+window_pos_y, duration=0.0)

    # 点击K线按钮
    def click_k_line_button(self):
        # 取截屏的前40像素:
        img_left_40_of_screen = self.frame[:,0:40]
        # 取匹配的坐标:
        pos = self.find_template_position(img_left_40_of_screen, self.template_k_line_button_idle)
        if pos != None: # 按钮已经按下
            self.click_button(pos[0]+14, pos[1]+14)
        else: # 按钮没有按下
            cv2.imwrite("get_F10_data_k_line_button_not_found_1.png", img_left_40_of_screen)
            # cv2.imwrite("get_F10_data_k_line_button_not_found_2.png", self.template_k_line_button_idle) # 模板一般不会出问题.
            raise Exception("!!!! ERR 507 K线按钮没找到, 没有K线按钮按下的情况才去找K线按钮的")

    def is_f10_still_idle(self):
        # return 如果有这个按钮, 返回按钮的坐标信息
        # self.template_F10_button_idle
        time.sleep(0.1)
        self.capture_window()
        # 裁剪出左侧36像素 实际截出来, 窗口范围更大,有截到左侧桌面背景, 所以加两像素 :
        img_left_36_of_screen = self.frame[:,0:38]
        # 判断模板self.template_F10_button_idle是否在img_left_36_of_screen中
        return self.find_template_position(img_left_36_of_screen, self.template_F10_button_idle)

    # 点击F10按钮
    def click_f10_button(self,pos):        
        self.click_button(pos[0]+pos[2]*.5, pos[1]+pos[3]*.5)

    def back_to_k_line(self):
        # 按F5切回K线图表
        pyautogui.press('f5')
        time.sleep(0.1)
        pyautogui.press('f5')
        time.sleep(0.1)

        # 查还原的结果是否正确
        # 完成还原后, 再回到上层调用
        # 这里曾因为没有再次截屏判断而卡死.
        self.capture_window()
        bool_is_restored = False
        while(False == bool_is_restored):
            bool_is_restored = self.is_k_line_button_activied()
            if False == bool_is_restored:
                # 试按一次F5
                # print("# 571 重试 按F5")
                pyautogui.press('f5')
                time.sleep(0.1)
                self.capture_window()
                bool_is_restored = self.is_k_line_button_activied()
                if False == bool_is_restored:
                    # 试用鼠标点击K线图按钮
                    self.click_k_line_button()
                    time.sleep(0.1)
                    self.capture_window()
                    # print("# 577 最后的手段, 用鼠标点")













            




    def get(self):
        
        print("# 433 get BEGIN")
        pyautogui.press('f10')

        # TODO::判断F10已按下, 有时不小心碰到键盘, 打开了输入数字的小窗口, 导致F10按下无效
        pos_button_f10 = self.is_f10_still_idle()
        if pos_button_f10 is not None:
            self.click_f10_button(pos_button_f10)


        bool_have_data_and_load_done_1 = False # 判断有数据
        retry_times = 10
        while False == bool_have_data_and_load_done_1 and retry_times > 0:
            time.sleep(0.2)
            retry_times-=1
            # 截取图像: self.frame
            self.capture_window()
            # 判断有数据:
            bool_have_data_and_load_done_1 = self.have_data_and_load_done_1()
            if False == bool_have_data_and_load_done_1:
                # 判断是否已经切换为F10页面
                self.is_F10_button_pressed_press_if_not_pressed()
                continue
            bool_have_data_and_load_done_1 = self.have_data_and_load_done_2()
        if retry_times<=0:
            self.back_to_k_line() # 还原到K线图
            return None # 出现过退市的股票也进到F10页面. 然后就卡死在这个页面.

        # TODO 记录F10第一页的数据 (第二张表还没做):
        logging.info("# 122 记录F10数据:")
        # 表1的字段表
        data_pageone_key_list = ["基本每股收益","扣非每股收益","稀释每股收益","总股本","总市值",
                                  "每股净资产","每股公积金","每股未分配利润","流通股本","流通市值",
                                  "每股经营现金流","市盈率动","市盈率TTM","市净率静","市净率"]
        data_pageone_key_value = [] # 键值对
        data_pageone = self.get_data_from_pageone()
        try_times_pageone = 10
        while None == data_pageone:
            print(f"# 515 重试 try_times_pageone:{try_times_pageone}")
            time.sleep(0.2)
            self.capture_window()
            data_pageone = self.get_data_from_pageone()
            try_times_pageone -= 1
            if 0 == try_times_pageone:
                logging.info("# 513 获取F10第一页数据失败")
                break
            # time.sleep(3)
        if None == data_pageone:
            logging.info("# 520 获取F10第一页数据失败")
            time.sleep(10)
        else:
            # print("# 520 data_pageone:",data_pageone)
            size_1 = len(data_pageone_key_list)
            size_2 = len(data_pageone)
            if 0 == size_2:
                raise Exception(f"!!!! ERR 566 失败了, 还执行下来  data_pageone:{data_pageone}")
            if size_1 != size_2:
                print("!!!!    563 pageone_key_list size != data_pageone size",f"\t\tsize_1:{size_1}  size_2:{size_2}")
            else:
                # print(f"# 569 size_1:{size_1}  size_2:{size_2}")
                for i in range(size_1):
                    # print(f"# 571 i:{i}")
                    data_pageone_key_value.append((data_pageone_key_list[i],data_pageone[i]))

        # # logging.info("# 445 self.get_data_from_pageone() 运行完")
        # time.sleep(0.1)

        self.back_to_k_line() # 还原到K线图

        # # 按F5切回K线图表
        # pyautogui.press('f5')
        # time.sleep(0.1)
        # pyautogui.press('f5')
        # time.sleep(0.1)
        # # 查还原的结果是否正确
        # # 完成还原后, 再回到上层调用
        # # 这里曾因为没有再次截屏判断而卡死.
        # self.capture_window()
        # bool_is_restored = False
        # while(False == bool_is_restored):
        #     bool_is_restored = self.is_k_line_button_activied()
        #     if False == bool_is_restored:
        #         # 试按一次F5
        #         # print("# 571 重试 按F5")
        #         pyautogui.press('f5')
        #         time.sleep(0.1)
        #         self.capture_window()
        #         bool_is_restored = self.is_k_line_button_activied()
        #         if False == bool_is_restored:
        #             # 试用鼠标点击K线图按钮
        #             self.click_k_line_button()
        #             time.sleep(0.1)
        #             self.capture_window()
        #             # print("# 577 最后的手段, 用鼠标点")

        # print("# 433 get END")
        return data_pageone_key_value







# 字符特征:
# 5: 0 1 2 3 4
# 7: 0 1 
# ----
# 1: 1 只要2这个位置是空就是1
# 2: 1 2
# 3: 1 2 7
# 8: 1 2 3
# 9: 1 2 3 4 
# ----
# 0: 2 
# 6: 2 后一列的 4 
# ----
# 4: 5
# -: 5
# ----
# ,: 8 
# .: 8 9
# ----
# 亿:11 不是万就是亿  宽度13
# 万:0              宽度13
# ----
# 英文字符方面, 只需遍历[0,1,2,5,8]这几个位置

# 2025-10-19
# 000061 农产品 这只股票的F10数据只有一个字段为空, 但目前的程序判断它为读取失败.
# 条件: None的计数只有1 算是能通过
# 条件: 第一张表的最后一个数据是 市净率, 必须不为None

# 2025-10-21
# 18:05 F10数据页面中, 平安银行也没有最新价, 这导致总市值这个字段没有数值. 为什么会有数据缺失?
#    会不会是被针对了?
#    是不是每天特定的时间会缺数据?
#    那些缺失的数据, 在K线图页面是能找到的.
#    因此 第一页第一张表, 一般会缺三个字段的数据.

# 2025-10-26
# BUG: 现在有几个崩溃问题, 表2为NULL, 以及切出来的列过宽. 这类问题都是难以触发, 也就难以修复. 现在已经可以跑完所有股票, 所以有可能一轮也取不到.
#       暂不中断程序, 有时是没加载完.

# 2025-10-27
# BUG: 没有切出列的情况, 会有好几个列并在一起, 对这个问题还在收集数据.


